home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / newlooklib.lha / newlook / createpalettebutton.c < prev    next >
C/C++ Source or Header  |  1993-11-07  |  2KB  |  107 lines

  1. /*
  2.  *  CREATEPALETTEBUTTON.C
  3.  */
  4.  
  5. #include "newlook.h"
  6.  
  7. static struct Border *CreatePaletteBorder(w,h,pen)
  8. WORD w,h;
  9. UBYTE pen;
  10. {
  11.   struct Border *b;
  12.   SHORT *xy;
  13.   int numpairs= 2 * ( (w<h) ? w:h );
  14.  
  15.   if(b= (struct Border *)SmartAllocate(BORDERSIZE))
  16.   {
  17.     if(xy= (SHORT *)SmartAllocate(2*numpairs*sizeof(SHORT)))
  18.     {
  19.       int n;
  20.  
  21.       b->LeftEdge   = 4;
  22.       b->TopEdge    = 2;
  23.       b->FrontPen   = pen;
  24.       b->BackPen    = 0;
  25.       b->DrawMode   = JAM1;
  26.       b->Count      = numpairs;
  27.       b->XY         = xy;
  28.       b->NextBorder = (struct Border *)NULL;
  29.  
  30.       if(w<h)
  31.       {
  32.         short x;
  33.         for(x=n=0; n<2*numpairs; n+=2)
  34.         {
  35.           xy[n]= x;
  36.           xy[n+1]= (n%4) ? (h-1):0;
  37.           if(n%4) x++;
  38.         }
  39.       }
  40.       else /* h<w */
  41.       {
  42.         short y;
  43.         for(y=n=0; n<2*numpairs; n+=2)
  44.         {
  45.           xy[n+1]= y;
  46.           xy[n]= (n%4) ? (w-1):0;
  47.           if(n%4) y++;
  48.         }
  49.       }
  50.     }
  51.     else b= (struct Border *)SmartFree(b);
  52.   }
  53.   return b;
  54. }
  55.  
  56.  
  57. struct Gadget *CreatePaletteButton(x,y,w,h,pen,id)
  58. WORD x,y,w,h;
  59. UBYTE pen;
  60. USHORT id;
  61. {
  62.   struct Gadget *g;
  63.   struct Border *b0, *b1, *p;
  64.  
  65.   ULONG UserHandle= SetNewLookHandle(PRIVATE_HANDLE);
  66.  
  67.   if(g= (struct Gadget *)SmartAllocate(GADGETSIZE))
  68.   {
  69.     if(b0= CreateBorder(0,0,w,h,FALSE))
  70.     {
  71.       if(b1= CreateBorder(0,0,w,h,TRUE))
  72.       {
  73.         if(p= (struct Border *)CreatePaletteBorder(w-8,h-4,pen))
  74.         {
  75.           LastBorder(b0)->NextBorder= p;
  76.  
  77.           /* LastBorder(b1)->NextBorder is NULL */
  78.  
  79.           g->NextGadget    = (struct Gadget *)NULL;
  80.           g->LeftEdge      = x;
  81.           g->TopEdge       = y;
  82.           g->Width         = w;
  83.           g->Height        = h;
  84.           g->Flags         = GADGHIMAGE;
  85.           g->Activation    = GADGIMMEDIATE|RELVERIFY;
  86.           g->GadgetType    = BOOLGADGET;
  87.           g->GadgetRender  = (APTR)b0;
  88.           g->SelectRender  = (APTR)b1;
  89.           g->GadgetText    = (APTR)NULL;
  90.           g->MutualExclude = (LONG)0L;
  91.           g->SpecialInfo   = (APTR)NULL;
  92.           g->GadgetID      = id;
  93.           g->UserData      = (APTR)p; /* NewLook private !!! */
  94.  
  95.           MakePrivateHandlePublic(UserHandle);
  96.           return g;
  97.         }
  98.       }
  99.     }
  100.   }
  101.   if(UserHandle != PRIVATE_HANDLE)
  102.   { SmartFreeAll(PRIVATE_HANDLE);
  103.     (void)SetNewLookHandle(UserHandle);
  104.   }
  105.   return (struct Gadget *)NULL;
  106. }
  107.